<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>文章管理</span>
      </div>
    </template>
    <!-- 搜索表单 -->
    <el-form inline>
      <el-form-item label="文章分类：">
        <el-select placeholder="请选择" v-model="categoryId" clearable style="width: 200px">
          <el-option label="技术分享" value="1"/>
          <el-option label="学习笔记" value="2"/>
          <el-option label="随心所写" value="3"/>
          <el-option label="剑指Offer" value="4"/>
        </el-select>
      </el-form-item>

      <el-form-item label="发布状态：">
        <el-select placeholder="请选择" v-model="status" clearable style="width: 200px">
          <el-option label="等待审核" value="0"></el-option>
          <el-option label="审核通过" value="1"></el-option>
          <el-option label="审核未通过" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getArticleList">搜索</el-button>
        <el-button @click="categoryId = null; status = null">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table stripe max-height="900" :data="articlelist"
              v-loading="loading"
              element-loading-text="Loading..."
              :element-loading-spinner="svg"
              element-loading-svg-view-box="-10, -10, 50, 50"
    >
      <el-table-column label="文章标题" prop="title"/>
      <el-table-column label="概述" prop="abstracts"/>
      <el-table-column label="封面">
        <template #default="scope">
          <el-popover
              placement="top-start"
              :width="200"
              trigger="hover"
          >
            <template #reference>
              <el-button class="m-2">查看封面</el-button>
            </template>
            <el-image :src="scope.row.coverurl"/>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="分类">
        <template #default="scope">
          <el-tag v-if="scope.row.category === 1" type="primary" size="large">技术分享</el-tag>
          <el-tag v-else-if="scope.row.category === 2" type="success" size="large">学习笔记</el-tag>
          <el-tag v-else-if="scope.row.category === 3" type="warning" size="large">随心所写</el-tag>
          <el-tag v-else-if="scope.row.category === 4" type="danger" size="large">剑指Offer</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="原文">
        <template #default="scope">
          <el-button type="primary" @click="getArticle(scope.row.id)">查看原文</el-button>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template #default="scope">
          <el-tag v-if="scope.row.status === 0" type="warning" size="large">审核中</el-tag>
          <el-tag v-else-if="scope.row.status === 1" type="success" size="large">审核通过</el-tag>
          <el-tag v-else-if="scope.row.status === 2" type="danger" size="large">未通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="点赞数" prop="likenumber"/>
      <el-table-column label="发布时间" prop="createtime"/>
      <el-table-column label="操作">
        <template #default="scope">
          <div v-if="scope.row.status === 0">
            <el-button type="primary" :icon="Edit" round @click="fun1(scope.$index,scope.row.id)">审核</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

<!--    抽屉查看文章内容-->
    <el-drawer direction="rtl" v-model="drawer" size="60%">
      <template #header>
        <h3>文章</h3>
      </template>
      <template #default>
        <!--                MdPreview标签用于展示内容  -->
        <MdPreview :editorId="id" v-model="content"/>

      </template>
    </el-drawer>

<!--    审核对话框-->
    <el-dialog
        v-model="dialogVisible"
        title="审核"
        width="500"
    >
      <el-form :model="auditresult" :rules="rules" ref="refuseForm" autocomplete="off" status-icon>
        <el-form-item label="id" prop="id">
          <el-input v-model="auditresult.id"/>
        </el-form-item>
        <el-form-item label="审核结果" prop="status">
          <el-select placeholder="请选择" v-model="auditresult.status" clearable>
            <el-option label="审核通过" value="1"></el-option>
            <el-option label="审核未通过" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="消息" prop="message">
          <el-input type="textarea" v-model="auditresult.message"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">Cancel</el-button>
          <el-button type="primary" @click="submitForm">
            Confirm
          </el-button>
        </div>
      </template>
    </el-dialog>

    <div style="display: flex;justify-content: center;">
      <el-pagination background layout="prev, pager, next,jumper"
                     v-model:page-size="pageSize"
                     v-model:current-page="pageNum"
                     :total="total"
                     @size-change="onSizeChange"
                     @current-change="onCurrentChange"
      />
    </div>
  </el-card>
</template>

<script setup>
// import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
import {
  Check,
  Delete,
  Edit,
  Message,
  Search,
  Star,
} from '@element-plus/icons-vue'
import {ref} from "vue"
import {articleListService,articleDetailsService,articleAuditService} from '/src/api/Admin.js'

import { MdPreview, MdCatalog } from 'md-editor-v3';
import 'md-editor-v3/lib/style.css'
import {ElMessage} from "element-plus";

const pageNum = ref(1)   //当前页
const total = ref(0)            //总条数
const pageSize = ref(10)  //每页条数

//用户搜索时选中的分类id
const categoryId = ref(null)
//用户搜索时选中的发布状态
const status = ref(null)

const id = 'preview-only'

const clickindex = ref()

//抽屉
const drawer = ref(false)

//审核对话框
const dialogVisible = ref(false)

//加载数据等待遮罩
const loading = ref(true)

const svg = `
        <path class="path" d="
          M 30 15
          L 28 17
          M 25.61 25.61
          A 15 15, 0, 0, 1, 15 30
          A 15 15, 0, 1, 1, 27.99 7.5
          L 15 15
        " style="stroke-width: 4px; fill: rgba(0, 0, 0, 0)"/>`

const articlelist = ref([])

const content = ref("")

// const ruleFormRef = ref<FormInstance>()

//获取表单
const refuseForm = ref(null) // form表单信息

// 在提交表单之前进行二次数据校验
const submitForm = () => {
  refuseForm.value.validate((valid) => {
    if (valid) {
      // 提交表单逻辑
      updateArticle()


    } else {
      ElMessage.error('验证失败!')
      return false
    }
  })
}


//表单规则
const rules = {
  id: [
    { required: true, message: '请输入ID', trigger: 'blur' }
  ],
  status: [
    { required: true, message: '请选择审核结果', trigger: 'change' }
  ],
  message: [
    { required: true, message: '请输入消息', trigger: 'blur' }
  ]
}

//审核结果
const auditresult = ref({
  id:null,
  status:null,
  message:""
})

//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
  pageSize.value = size
  getArticleList()

}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
  pageNum.value = num
  getArticleList()

}

//获取文章列表
const getArticleList = async () => {

  let params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
    categoryId: categoryId.value ? categoryId.value : null,
    status: status.value ? status.value : null
  }

  let result = await articleListService(params)

  articlelist.value = result.data.items;
  total.value = result.data.total;
  loading.value = false

}

const fun1 = (index,id) => {

  clickindex.value = index

  auditresult.value.id = id
  dialogVisible.value = true

}

// 获取文章内容
const getArticle = async (id) => {

  let result = await articleDetailsService(id)

  content.value = result.data

  drawer.value = true

}

// 审核文章
const updateArticle = async () => {

  if(auditresult.value.id === null || auditresult.value.status === null || auditresult.value.message === ''){
    ElMessage.error('请填写完整信息!')
    return
  }

  let result = await articleAuditService(auditresult)

  dialogVisible.value = false

  articlelist.value[clickindex.value].status = auditresult.value.status

  ElMessage.success('操作成功!')

}
</script>

<style scoped>
.page-container{
  min-height: 100%;
  box-sizing: border-box;
}

</style>